//
//  ASDoutuController.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASDoutuController.h"
#import "TYPagerController.h"
#import "ASDoutuHeadMenuView.h"
#import "ASDoutuSubController.h"
#import "ASHomeApi.h"
#import "ASDoutuHeadModel.h"
#import "ASNoneView.h"

static NSString *ASDoutuSubControllerStr = @"ASDoutuSubController";

@interface ASDoutuController () <TYPagerControllerDataSource,TYPagerControllerDelegate>

@property (nonatomic, weak) TYPagerController *pagerController;
@property (nonatomic, strong) NSArray *menuData;
@property (nonatomic, strong) ASDoutuHeadMenuView *menuView;
@property (strong, nonatomic) ASNoneView *noneView;


@end

@implementation ASDoutuController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"斗图表情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.noneView];
    [self addPagerController];
    [self loadData];
    [self showNoneView];
    [self.noneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [ASTool addUMTongJiKey:@"home" subKey:@"expression" titleStr:@"总浏览量"];

    // Do any additional setup after loading the view.
}

- (void)addPagerController {
    TYPagerController *pagerController = [[TYPagerController alloc]init];
    pagerController.layout.prefetchItemCount = 1;
    //pagerController.layout.autoMemoryCache = NO;
    // 只有当scroll滚动动画停止时才加载pagerview，用于优化滚动时性能
    pagerController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    pagerController.dataSource = self;
    pagerController.delegate = self;
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    [pagerController registerClass:[ASDoutuSubController class] forControllerWithReuseIdentifier:ASDoutuSubControllerStr];
    self.pagerController = pagerController;
}

- (void)showNoneView{
    if (self.menuData.count > 0) {
        self.pagerController.view.hidden = NO;
        self.noneView.hidden = YES;
        self.menuView.hidden = NO;
    }else{
        self.pagerController.view.hidden = YES;
        self.noneView.hidden = NO;
        self.menuView.hidden = YES;
    }
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.pagerController.view.frame = CGRectMake(0, CGRectGetMaxY(self.menuView.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)- CGRectGetMaxY(self.menuView.frame));
}

- (ASDoutuHeadMenuView *)menuView{
    if (!_menuView) {
        _menuView = [[ASDoutuHeadMenuView alloc]initWithFrame:CGRectZero];
        _menuView.backgroundColor = self.view.backgroundColor;
        @weakify(self);
        _menuView.clickSubIndexBlock = ^(NSInteger index) {
            @strongify(self);
            if (self.menuData.count > index) {
                ASDoutuHeadModel *subM = self.menuData[index];
                [ASTool addUMTongJiKey:@"home" subKey:@"expression" titleStr:subM.name];
            }

            [self.pagerController scrollToControllerAtIndex:index animate:YES];
        };
    }
    return _menuView;
}

- (ASNoneView *)noneView{
    if (!_noneView) {
        _noneView = [[ASNoneView alloc]initWithFrame:CGRectZero];
        [_noneView.logoImage setImage:[UIImage imageNamed:@"me_wsj"]];
        _noneView.titleLab.text = @"暂时没有相关数据";
    }
    return _noneView;
}

- (void)loadData{
    [ASHomeApi loadDoutuDataCompletion:^(ASBaseModel *  _Nonnull result) {
        if (result.state.intValue == 1) {
            NSMutableArray *arr = [NSMutableArray new];
            ASDoutuHeadModel *model1 = [ASDoutuHeadModel new];
            model1.ifSelect = YES;
            model1.imageStr = @"expression_jx";
            model1.name = @"精选";
            model1.tID = @"0";
            [arr addObject:model1];
            
            ASDoutuHeadModel *model2 = [ASDoutuHeadModel new];
            model2.imageStr = @"expression_zx";
            model2.name = @"最新";
            model2.tID = @"1";
            [arr addObject:model2];
            
            NSArray *imageArr = @[@"expression_mx",@"expression_ys",@"expression_bq"];
            for (int x = 0; x < result.resultArr.count; x++) {
                ASDoutuHeadModel *model = result.resultArr[x];
                if (imageArr.count > x) {
                    model.imageStr = imageArr[x];
                }
                [arr addObject:model];
            }
            
            NSInteger index = arr.count;
            if (index < 1) {
                index = 1;
            }
            CGFloat w = (SCREEN_WIDTH - 26 - (index-1)*6)/arr.count;
            self.menuView.frame = CGRectMake(0, Height_NavBar, SCREEN_WIDTH, w + 20);
            self.pagerController.view.frame = CGRectMake(0, CGRectGetMaxY(self.menuView.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)- CGRectGetMaxY(self.menuView.frame));

            self.menuView.itemW = w;
            self.menuData = arr;
            self.menuView.headArr = arr;
            [ASTool addUMTongJiKey:@"home" subKey:@"expression" titleStr:@"精选"];
            [self.pagerController reloadData];
        }else{
            [MBProgressHUD showError:result.errorMsg];
        }
        [self showNoneView];
    } failure:^(NSError * _Nonnull error) {
        [MBProgressHUD showError:FENETERROR];
        [self showNoneView];
    }];
    
    
}

#pragma mark - TYPagerControllerDataSource

- (NSInteger)numberOfControllersInPagerController {
    return self.menuData.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
//    ASDoutuSubController *VC = [pagerController dequeueReusableControllerWithReuseIdentifier:ASDoutuSubControllerStr forIndex:index];
    ASDoutuSubController *VC = [[ASDoutuSubController alloc]init];
    if (self.menuData.count > index) {
        VC.index = index;
        VC.model = self.menuData[index];
    }
    return VC;
}

#pragma mark - TYPagerControllerDelegate

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [self.menuView setScrollIndex:toIndex ifBlock:NO];
}

-(void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
//    [self.menuView setScrollIndex:toIndex ifBlock:NO];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
